package com.cipher;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class CipherUtils {
    private static CipherUtils mCipherUtils;
    private Cipher encryptCipher = null;
    private Cipher decryptCipher = null;

    public static CipherUtils getInstance() {
        if (mCipherUtils == null) {
            try {
                mCipherUtils = new CipherUtils();
            } catch (Exception var1) {
                var1.printStackTrace();
            }
        }

        return mCipherUtils;
    }

    public static native String getCipherKeyFromJNI();

    private CipherUtils() {
        String keyString = getCipherKeyFromJNI();

        try {
            Key key = this.getKey(keyString.getBytes());
            this.encryptCipher = Cipher.getInstance("DES");
            this.encryptCipher.init(1, key);
            this.decryptCipher = Cipher.getInstance("DES");
            this.decryptCipher.init(2, key);
        } catch (Exception var3) {
            var3.printStackTrace();
        }

    }

    private String byteArr2HexStr(byte[] arrB) throws Exception {
        int iLen = arrB.length;
        StringBuffer sb = new StringBuffer(iLen * 2);

        for(int i = 0; i < iLen; ++i) {
            int intTmp;
            for(intTmp = arrB[i]; intTmp < 0; intTmp += 256) {
            }

            if (intTmp < 16) {
                sb.append("0");
            }

            sb.append(Integer.toString(intTmp, 16));
        }

        return sb.toString();
    }

    private byte[] hexStr2ByteArr(String strIn) throws Exception {
        byte[] arrB = strIn.getBytes();
        int iLen = arrB.length;
        byte[] arrOut = new byte[iLen / 2];

        for(int i = 0; i < iLen; i += 2) {
            String strTmp = new String(arrB, i, 2);
            arrOut[i / 2] = (byte)Integer.parseInt(strTmp, 16);
        }

        return arrOut;
    }

    public byte[] encrypt(byte[] arrB) throws Exception {
        return this.encryptCipher.doFinal(arrB);
    }

    public String encrypt(String strIn) throws Exception {
        return this.byteArr2HexStr(this.encrypt(strIn.getBytes()));
    }

    public byte[] decrypt(byte[] arrB) throws Exception {
        return this.decryptCipher.doFinal(arrB);
    }

    public String decrypt(String strIn) throws Exception {
        return new String(this.decrypt(this.hexStr2ByteArr(strIn)));
    }

    private Key getKey(byte[] arrBTmp) throws Exception {
        byte[] arrB = new byte[8];

        for(int i = 0; i < arrBTmp.length && i < arrB.length; ++i) {
            arrB[i] = arrBTmp[i];
        }

        Key key = new SecretKeySpec(arrB, "DES");
        return key;
    }

    public static String getMD5(byte[] source) {
        String s = null;

        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(source);
            byte[] tmp = md.digest();
            s = byteArrayToHexString(tmp);
        } catch (Exception var4) {
            var4.printStackTrace();
        }

        return s;
    }

    private static String byteArrayToHexString(byte[] bytes) {
        StringBuffer hex_string = new StringBuffer(bytes.length * 2);
        if (bytes != null) {
            for(int i = 0; i < bytes.length; ++i) {
                int byte_val = bytes[i];
                if (byte_val < 0) {
                    byte_val += 256;
                }

                String hex_code = Integer.toHexString(byte_val);
                if (hex_code.length() % 2 == 1) {
                    hex_code = "0" + hex_code;
                }

                hex_string.append(hex_code);
            }
        }

        return hex_string.toString();
    }

    public static String getCertificateSHA1Fingerprint(Context context) {
        try {
            if (context == null) {
                return "";
            } else {
                PackageManager pm = context.getPackageManager();
                String packageName = context.getPackageName();
                int flags = 64;
                PackageInfo packageInfo = null;

                try {
                    packageInfo = pm.getPackageInfo(packageName, flags);
                } catch (NameNotFoundException var17) {
                    var17.printStackTrace();
                }

                Signature[] signatures = packageInfo.signatures;
                byte[] cert = signatures[0].toByteArray();
                InputStream input = new ByteArrayInputStream(cert);
                CertificateFactory cf = null;

                try {
                    cf = CertificateFactory.getInstance("X509");
                } catch (CertificateException var16) {
                    var16.printStackTrace();
                }

                X509Certificate c = null;

                try {
                    c = (X509Certificate)cf.generateCertificate(input);
                } catch (CertificateException var15) {
                    var15.printStackTrace();
                }

                String hexString = null;

                try {
                    MessageDigest md = MessageDigest.getInstance("SHA1");
                    byte[] publicKey = md.digest(c.getEncoded());
                    hexString = byte2HexFormatted(publicKey);
                } catch (NoSuchAlgorithmException var13) {
                    var13.printStackTrace();
                } catch (CertificateEncodingException var14) {
                    var14.printStackTrace();
                }

                return hexString;
            }
        } catch (Exception var18) {
            var18.printStackTrace();
            return "";
        }
    }

    public static String byte2HexFormatted(byte[] arr) {
        StringBuilder str = new StringBuilder(arr.length * 2);

        for(int i = 0; i < arr.length; ++i) {
            String h = Integer.toHexString(arr[i]);
            int l = h.length();
            if (l == 1) {
                h = "0" + h;
            }

            if (l > 2) {
                h = h.substring(l - 2, l);
            }

            str.append(h.toUpperCase());
            if (i < arr.length - 1) {
                str.append(':');
            }
        }

        return str.toString();
    }

    static {
        System.loadLibrary("cipher-jni");
    }
}
